<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>增加特采申请 - IFS备货预测系统</title>
<link rel="shortcut icon" href="favicon.ico">
<!-- FontAwesome 4.3.0 -->
<link href="{$fonts.url}font-awesome-4.4.0/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
<!-- Ionicons 2.0.1 -->
<link href="{$fonts.url}ionicons-2.0.1/css/ionicons.min.css" rel="stylesheet" type="text/css" />
<!--bootstrap-->
<link href="{$css.url}bootstrap_min.css" rel="stylesheet" type="text/css" />
<!--AdminLTE theme-->
<link href="{$css.url}AdminLTE_min.css" rel="stylesheet" type="text/css" />
<link href="{$css.url}AdminLT_skins/_all-skins.min.css" rel="stylesheet" type="text/css" />	
<!--datapicker-->
<link href="{$js.url}plugins/datapicker/datepicker.min.css" rel="stylesheet" type="text/css" />
<!--tag edit-->
<link href="{$css.url}jquery.tag-editor.css" rel="stylesheet" type="text/css" />
<!--web css-->
<link rel="stylesheet" href="{$css.url}product_manage_min.css">
<link href="{$css.url}bootstrap-datetimepicker.min.css" rel="stylesheet" type="text/css" />
<!--[if lt IE 9]>
    <script src="{$js.url}plugins/html5shiv.min.js"></script>
    <script src="{$js.url}plugins/respond.min.js"></script>
<![endif]-->
</head>
<style>
	textarea{
		resize:none;
	}
	.select2-container .select2-selection--single{
		height:25px;
		border-radius: 0px;
    	border-color: rgb(169, 169, 169);
	}
	.select2-container--default .select2-selection--single .select2-selection__rendered{
		height:25px;
	}
</style>
<body class="skin-blue">
<div class="wrapper">
	{include file="bread_crumbs.php"}
	<!--引入面包屑-->
	<section class="base-setting-wrap">
		<div class="row">
			<div class="col-md-12">
				<div class="box">
					<form action="/purchase/index/doadd" auto-url="" id="js_form">
		                <div class="box-body">
		                  <table class="table table-bordered table-striped table-hover" id="js_productTable">
	                    	<tbody>
			                    <tr>
			                    	<td class="text-right" width="148"><b>产品编码：<em class="text-star">*</em></b></td>
			                    	<td class="text-left">
			                    		<input type="text" id="goods_sn" name="goods_sn" url="/purchase/index/beforeadd"> <span class="goods_sn" style="color:red;"></span> 
			                    	</td>
								</tr>
								<tr>
			                    	<td class="text-right"><b>产品名称：<em class="text-star">*</em></b></td>
			                    	<td class="text-left">
			                    		<input type="text" id="product_name" name="product_name" style="width:30%;" /> <span class="product_name" style="color:red;"></span>
			                    	</td>
								</tr>
								<tr>
									<td class="text-right"><b>备货数量：<em class="text-star">*</em></b></td>
									<td class="text-left">
										<input type="text" name="apply_num" id="apply_num" /> <span class="apply_num" style="color:red;"></span>
									</td>
								</tr>
								<tr>
			                    	<td class="text-right"><b>备货类型：<em class="text-star">*</em></b></td>
			                    	<td class="text-left">
			                    		<select name="stocking_type" id="stocking_type">
			                    			<option value="">请选择</option>
			                    			<option value="1">正常备货</option>
			                    			<option value="2">活动备货（第三方平台）</option>
			                    			<option value="3">活动备货（自营平台）</option>
			                    			<option value="4">活动备货（其他）</option>
			                    		</select>
			                    		<span class="stocking_type" style="color:red;"></span>
			                    	</td>
								</tr>
								<tr>
									<td class="text-right"><b>存放仓库：<em class="text-star">*</em></b></td>
			                    	<td class="text-left">
										<select id="warehouse_id" name="warehouse_id">
											<option value="">请选择</option>
											{foreach from=$depts item="value"}
												<option value="{$value.id}" data-code="{$value.warehouse_code}">{$value.warehouse_name}</option>
											{/foreach}
										</select>
										<span class="warehouse_id" style="color:red;"></span>
									</td>
								</tr>
								<tr id="plactform_selection" style="display:none">
									<td class="text-right"><b>平台仓：<em class="text-star">*</em></b></td>
			                    	<td class="text-left">
										<select name="platform_code" id="platform_code">
											<option value="">请选择</option>
											{foreach from=$warehouse_platform item="value"}
												<option value="{$value.warehouse_code}" data-id="{$value.id}">{$value.warehouse_name}</option>
											{/foreach}
										</select>
										<span class="platform_code" style="color:red;"></span>
									</td>
								</tr>
								<tr>
			                    	<td class="text-right"><b>最迟回货时间：<em class="text-star">*</em></b></td>
			                    	<td class="text-left">
			                    		<input type="text" name="latest_time" class="js_datePicker date_start" id="latest_time"> <span class="latest_time" style="color:red;">【该项必填】 即原截止日期，决定是否在ERP产生需求B，若超过最迟回货时间仍未回货，则不再产生需求。</span>
			                    	</td>
								</tr>
								<tr>
			                    	<td class="text-right"><b>预估销售时间段：<em class="text-star">*</em></b></td>
			                    	<td class="text-left">
			                    		<input type="text" class="js_datePicker date_start" name="expect_from" id="expect_from"> - <input type="text"  class="js_datePicker date_end" name="expect_to" id="expect_to"> <span class="expect_sale_num" style="color:red;">【该项必填】指本次备货用来进行销售的时间范围的预估值。</span>
			                    	</td>
								</tr>
								<tr>
			                    	<td class="text-right"><b>预估总销量：<em class="text-star">*</em></b></td>
			                    	<td class="text-left">
			                    		<input type="text" name="expect_sale_num" id="expect_sale_num"><span style="color:red;">【 该项必填】指在预计销售时间范围内的总销量的估算值。</span>
			                    	</td>
								</tr>
								<tr>
									<td class="text-right">备注：</td>
									<td class="text-left"><textarea style="width:50%;" name="remark"></textarea></td>
								</tr>
								<tr>
									<td class="text-right">销售人员：</td>
									<td class="text-left"><input type="text" name="sale_user"></td>
								</tr>
							</tbody>
		              	</table>
		                </div>
	                </form>
	                <!-- .box-body -->
	                <div class="box-footer text-center" style="position:relative">
						<button type="button" class="btn btn-primary btn-sm" id="search_button">确定</button>
						<a href="" class="btn btn-default btn-sm">重置</a>
						<span id="message"></span>
	                </div>
	              </div>
			</div>
		</div>
	</section>
</div>
<script src="{$js.url}lib/jquery-1.9.1.min.js" type="text/javascript"></script>
<script src="{$js.url}lib/bootstrap.min.js" type="text/javascript"></script>
<script src="{$js.url}plugins/layer.js" type="text/javascript"></script>
<script src="{$js.url}purchase/common.js" type="text/javascript"></script>
<!-- 日期 -->
<script src="{$js.url}plugins/bootstrap-datetimepicker.min.js" type="text/javascript"></script>
<script src="{$js.url}linkagesel-min.js" type="text/javascript"></script>
<!--common js-->
<script src="{$js.url}common.js"></script>
<link rel="stylesheet" type="text/css" href="{$js.url}plugins/select2/css/select2.min.css">	
<script type="text/javascript" src="{$js.url}plugins/select2/js/select2.min.js"></script>
<script>
{literal}
$(function (){
	var is_haitao = 0;
	$('input[name="goods_sn"]').blur(function (){
		if($('#goods_sn').val() == ''){
			$('.goods_sn').text('请输入SKU........');
			return false;
		}
		$('#plactform_selection').css({'display':'none'});
		$.get($('#goods_sn').attr('url'), 'goods_sn=' + $('#goods_sn').val(), function (rel){
			$('.goods_sn').text('');
			$('.goods_sn').attr('shelf_time', rel.pro.shelf_time);
			if(!rel.pro){
				$('.goods_sn').text('SKU不存在！');
				return false;
			}
			if(rel.pro.goods_type == 3){
				$('.goods_sn').text('禁止添加捆绑商品，请');
				return false;
			}
			$('#product_name').val(rel.pro.product_name);
			$('#product_name').attr('readonly', 'readonly');
			if(rel.pro.edit_status != '4' && rel.pro.edit_status != '8' && rel.pro.edit_status != '10'){
				$('.goods_sn').text('只限添加审核通过的产品！');
				return false;
			}
			if(rel.pro.goods_resource_status == '3'){
				$('.goods_sn').text('不能添加停产产品！');
				return false;
			}
			if(rel.haitao == 1){
				is_haitao = 1;
				$('#plactform_selection').show();
				return false;
			}
		}, 'json');
	});
	$('#warehouse_id').select2();
	// 切换仓库时进行存销比判断
	$('#warehouse_id').change(function (){
		if($('#warehouse_id option:selected').val() == ''){
			return false;
		}
		if(is_haitao){
			return false;
		}else{
			var selected = $(this).find('option:selected').attr('data-code');
		}
		stock_sale(selected,'warehouse_id');
	});
	$("#platform_code").change(function(){
		var selected = $("#platform_code").val();
		if(!selected) {
			return false;
		}
		stock_sale(selected,'platform_code');
	});
	$("#apply_num").blur(function(){
		if($('#warehouse_id option:selected').val() == ''){
			return false;
		}
		if(is_haitao){
			var selected = $("#platform_code").val();
			if(!selected) {
				return false;
			}
			stock_sale(selected,'platform_code');
		}else{
			var selected = $('#warehouse_id option:selected').attr('data-code');
			stock_sale(selected,'warehouse_id');
		}
	});
	function stock_sale(warehouse,class_name) {
		$('.'+class_name).text('');
		$('#message').attr('propersion', '0'); // 当备货量大于20时 进行存销比判断
		
		var timeStamp = Date.parse(new Date());
		timeStamp = timeStamp / 1000;
		var shelf_time = Date.parse($('.goods_sn').attr('shelf_time'));
		shelf_time = shelf_time / 1000;
		if(parseInt($('#apply_num').val()) > 20 || (timeStamp - shelf_time) > 3600 * 24 * 30){
			$('.'+class_name).text('');
			$.get(
				'/purchase/index/checkpurchase',
				{goods_sn:$('#goods_sn').val(), warehouse_code:warehouse},
				function (result){
					if(parseInt(result['pro']) > 3){
						$('.'+class_name).text('注意：当前商品的存销比大于3！');
						$('#message').attr('propersion', '1'); // 当备货量大于20时 进行存销比判断
					}
					return false;
			}, 'json');
		}
	}
	$('#latest_time').change(x.check_latest_time);
	$('#expect_from').change(x.expect_from);
	$('#expect_to').change(x.expect_to);
	$('#search_button').click(function (){
		if(!$('#goods_sn').val()){
			layer.msg('请填写SKU！');
			return false;
		}
		$.get($('#goods_sn').attr('url'), 'goods_sn=' + $('#goods_sn').val(), function (rel){
			if(!rel.pro){
				layer.msg('SKU不存在！');
				return false;
			}
			if(!$('#product_name').val()){
				layer.msg('请填写产品名称！');
				return false;
			}
			if(rel.pro.edit_status != '4' && rel.pro.edit_status != '8' && rel.pro.edit_status != '10'){
				layer.msg('只限添加审核通过的产品！');
				return false;
			}
			if(rel.pro.goods_resource_status == '3'){
				layer.msg('不能添加停产产品！');
				return false;
			}
			if(!$('#apply_num').val()){
				layer.msg('请填写备货数量！');
				return false;
			}
			if(isNaN($('#apply_num').val())){
				layer.msg('备货数量应该填写阿拉伯数字！');
				return false;
			}
			if(parseInt($('#apply_num').val()) < 0){
				layer.msg('备货数量不能输入负数！');
				return false;
			}
			if(!$('select[name="stocking_type"]').val()){
				layer.msg('请选择备货类型！');
				return false;
			}
			if(!$('#warehouse_id option:selected').val() == '-'){
				layer.msg('请选择存放仓库');
				return false;
			}
			if(!$('#latest_time').val()){
				layer.msg('请填写最迟回货时间！');
				return false;
			}
			if(!$('#expect_from').val() || !$('#expect_to').val()){
				layer.msg('请完整填写选择预估销售时间段！');
				return false;
			}
			if(!$('#expect_sale_num').val()){
				layer.msg('请填写预估总销量！');
				return false;
			}
			if(parseInt($('#expect_sale_num').val()) < parseInt($('#apply_num').val())){
				layer.msg('预估总销量必须大于备货销量');
				return false;
			}
			if(isNaN($('#expect_sale_num').val())){
				layer.msg('预估总销量请填写阿拉伯数字！');
				return false;
			}
			if($('#message').attr('propersion') == 1){
				layer.confirm('当前产品的存销比大于3，确定提交？', function (){
					post();
					return false;
				});
			}else{
				post();
				return false;
			}
		}, 'json');
	});
	function post(){
		$.ajax({
			url : $('#js_form').attr('action'),
			data : $('#js_form').serialize() + '&warehouse_code=' + $('#warehouse_id option:selected').attr('data-code') + '&stocking_type_char=' + $('select[name="stocking_type"] option:selected').text(),
			dataType : 'json',
			type : 'post',
			success : function (rel){
				if(rel.status == 1){
					layer.alert('添加完成，点击确定之后跳转至列表...', function (){
						window.location.href="/purchase/";
					});
					return false;
				}
				layer.alert(rel.message);
			}
		});
	}
	// 日期选择
	$('.date_start').datetimepicker({
	    format: "yyyy-mm-dd",
        minView:2,
        autoclose: true,
        todayHighlight: false
	}).on('changeDate', function(ev){
        $(".date_end").val("").datetimepicker('setStartDate',$(this).val());
    });
    $('.date_end').datetimepicker({
	    format: "yyyy-mm-dd",
        minView:2,
        autoclose: true,
        todayHighlight: false
	});
})
{/literal}
</script>
</body>
</html>
